home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 73700.776@compuserve.com (Walter C. Riley)
- Newsgroups: comp.lang.c++
- Subject: Re: usage of ostrstream
- Date: Tue, 23 Jan 1996 21:37:30 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4e3kgr$90r@dub-news-svc-6.compuserve.com>
- References: <4e17t2$24v@crchh327.rich.bnr.ca>
- NNTP-Posting-Host: ad45-166.compuserve.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Mike Hall <arnab@bnr.ca> wrote:
-
- >Hi,
- >This is beginers query but any help is highly appreciated.
- >I am working on C++ in HP-UX environment.
- >I am trying to something like this in my code
- >f()
- >{
- > ostrstream oss;
-
- > oss<<"The first string"<<ends
- > cout << oss.str()<<endl;
-
- > oss.seekp(0);
- > oss<<"The second string in the same oss"<<ends;
- > cout << oss.str()<<endl;
- >}
-
- >My test tool shows that the above has memory leaks.
- >On reading the man pages I found that once .str() is
- >called the buffer freezes and further usage of the oss
- >object is unpredictable.Also the user should delete the
- >char * returned by the .str().
-
- >What I'd like to know is, a way to use/access the ostrstream
- >string multiply in a function without having to create a
- >ostrstream type object everytime.
-
- >Please E-Mail to arnab@bnr.ca
-
- >Thanks in advance for your help/suggestions
- >Arnab
-
- Amab,
- Try something like this:
-
- char buf[1000];
- ostrstream oss(buf, sizeof(buf));
- oss << "The first string" << ends;
- cout << buf << endl;
- oss.seekp(0);
- oss<<"The second string in the same oss"<<ends;
- cout << buf << endl;
-
- if you know the approx size of buf that you will need and it is not so
- large as to overflow the stack. If buf will be very large, you can
- use new to create buf, but remember to delete it when you are done.
-
- Good luck,
- Walt
-
-